package com.tax;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.ijpay.entity.WxPayBean;
import com.jpay.ext.kit.PaymentKit;
import com.jpay.util.HttpUtils;
import com.jpay.util.StringUtils;
import com.tax.dao.PersonTaxDao;
import com.tax.model.PersonTax;
import com.tax.model.ResponseData;
import com.util.IDCardUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by castiel on 2019/01/02 0002.
 */
@RestController
@RequestMapping("api")
@Api("swaggerDemoController相关的api")
@Slf4j
public class TaxController {
    @Autowired
    private PersonTaxDao persionTaxDao;
    @Autowired
    private WxPayBean wxPayBean;

    //新增
    @ApiOperation(value = "新增", notes = "新增个税")
    @RequestMapping(value="/save", method= RequestMethod.POST)
    public ResponseData saveNewContact(@RequestBody PersonTax personTax) {
        if(null == personTax.getOpenid()){
            ResponseData.error("没有唯一值");
        }
        if(null != personTax.getId_card() && !IDCardUtil.isIDCard(personTax.getId_card())){
            return ResponseData.error("身份证不合法");
        }
        if(StringUtils.isBlank(personTax.getId_card()) && StringUtils.isBlank(personTax.getPassport())){
            return ResponseData.error("身份证与护照必须填一个");

        }
        PersonTax person=this.persionTaxDao.getByOpenid(personTax.getOpenid());
        if(null ==person){
            person=this.persionTaxDao.save(personTax);
        }
        return ResponseData.success(person);
    }

    @ApiOperation(value = "新增", notes = "修改个税")
    @RequestMapping(value="/update", method= RequestMethod.POST)
    public ResponseData update(@RequestBody PersonTax personTax) {
        if(null == personTax.getId()){
            return ResponseData.error("没有主键");
        }
        if(null != personTax.getId_card() && !IDCardUtil.isIDCard(personTax.getId_card())){
            return ResponseData.error("身份证不合法");
        }
        if(StringUtils.isBlank(personTax.getId_card()) && StringUtils.isBlank(personTax.getPassport())){
            return ResponseData.error("身份证与护照必须填一个");
        }
        personTax=this.persionTaxDao.save(personTax);
        return ResponseData.success(personTax);
    }

    @ApiOperation(value = "获取个税详细", notes = "新增个税")
    @RequestMapping(value="/getPersonTax", method= RequestMethod.POST)
    public ResponseData getPersonTax(@RequestBody PersonTax personTaxd) {
        personTaxd=this.persionTaxDao.getByOpenid(personTaxd.getOpenid());
        int sum=0;
        if(null !=personTaxd){
            if(null !=personTaxd.getChildren_education() && personTaxd.getChildren_education().length()>0){
                sum+=Integer.parseInt(personTaxd.getChildren_education());
            }
            if(null !=personTaxd.getContinuing_education() && personTaxd.getContinuing_education().length()>0){
                sum+=Integer.parseInt(personTaxd.getContinuing_education());
            }
            if(null !=personTaxd.getHourse_loan() && personTaxd.getHourse_loan().length()>0){
                sum+=Integer.parseInt(personTaxd.getHourse_loan());
            }
            if(null !=personTaxd.getRent_hourse() && personTaxd.getRent_hourse().length()>0){
                sum+=Integer.parseInt(personTaxd.getRent_hourse());
            }
            if(null !=personTaxd.getSolatium() && personTaxd.getSolatium().length()>0){
                sum+=Integer.parseInt(personTaxd.getSolatium());
            }
            if(null !=personTaxd.getMedical_illness() && personTaxd.getMedical_illness().length()>0){
                sum+=Integer.parseInt(personTaxd.getMedical_illness());
            }
            personTaxd.setSum(sum);
        }

        return ResponseData.success(personTaxd);
    }

    @ApiOperation("小程序授权--通过code拿到用户基础信息,并返回token、session_key")
    @RequestMapping(value = "/code2Session",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData getCode(@RequestParam("code") String code){
        return this.code2Session(code);
    }

    private ResponseData code2Session(String code) {
        //1. 通过code等访问微信小程序服务器 拿到openid(其他信息只能通过前端返回)
        //2. 该用户有系统记录，则返回token,若无记录 则生成一条记录
        //GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
        Map<String,String> params = new HashMap<>();
        params.put("appid",wxPayBean.getAppId());
        params.put("secret", wxPayBean.getAppSecret());
        params.put("js_code", code);
        params.put("grant_type", "authorization_code");
        String result = HttpUtils.get("https://api.weixin.qq.com/sns/jscode2session",params);
        Map<String, Object> temp = (Map)JSON.parse(result);
        Integer errcode = (Integer)temp.get("errcode");
        if (null == errcode){
            String openid = (String)temp.get("openid");
            Map<String,String> responseData = Maps.newHashMap();
            responseData.put("openid",openid);
            return ResponseData.success(responseData);
        }else {
            log.error("======【code2Session请求错误】=======errMsg:{}",temp.get("errmsg"));
        }
        return null;
    }
}
